# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 15, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION, GOVERNMENT JOBS: DEMOCRATS 

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

#######MATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==1 & democrat==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (govjobs_LC=="Liberal" & abortion_LC=="Liberal"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$abortion_LC))
our.props1$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==0.5 & democrat==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (govjobs_LC=="Liberal" & abortion_LC=="Liberal"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$abortion_LC))
our.props2$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==0 & democrat==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (govjobs_LC=="Liberal" & abortion_LC=="Liberal"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$abortion_LC))
our.props3$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props3$year))
our.props3
new.data1 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION, AID TO BLACKS: DEMOCRATS

#######MATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==1 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (abortion_LC=="Liberal" & aidblacks_LC=="Liberal"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$abortion_LC))
our.props1$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0.5 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (abortion_LC=="Liberal" & aidblacks_LC=="Liberal"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$abortion_LC))
our.props2$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (abortion_LC=="Liberal" & aidblacks_LC=="Liberal"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$abortion_LC))
our.props3$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props3$year))
our.props3
new.data2 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT JOBS, AID TO BLACKS: DEMOCRATS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

#######MATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==1 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (aidblacks_LC=="Liberal" & govjobs_LC=="Liberal"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$govjobs_LC))
our.props1$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0.5 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (aidblacks_LC=="Liberal" & govjobs_LC=="Liberal"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$govjobs_LC))
our.props2$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0 & democrat==1 & whiteNH==1))
our.props
# PROPORTION WITH LIBERAL ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (aidblacks_LC=="Liberal" & govjobs_LC=="Liberal"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$govjobs_LC))
our.props3$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props3$year))
our.props3
new.data3 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION, GOVERNMENT JOBS: REPUBLICANS 

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

#######MATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==1 & republican==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (govjobs_LC=="Conservative" & abortion_LC=="Conservative"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$abortion_LC))
our.props1$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==0.5 & republican==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (govjobs_LC=="Conservative" & abortion_LC=="Conservative"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$abortion_LC))
our.props2$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ abortion_LC + govjobs_LC + year, design = subset(our.design, programmatic==0 & republican==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (govjobs_LC=="Conservative" & abortion_LC=="Conservative"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$abortion_LC))
our.props3$statistic.name = rep("3. Proportion taking party's position on both \nabortion and government jobs", length(our.props3$year))
our.props3
new.data4 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION, AID TO BLACKS: REPUBLICANS

#######MATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==1 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (abortion_LC=="Conservative" & aidblacks_LC=="Conservative"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$abortion_LC))
our.props1$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0.5 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (abortion_LC=="Conservative" & aidblacks_LC=="Conservative"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$abortion_LC))
our.props2$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ abortion_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (abortion_LC=="Conservative" & aidblacks_LC=="Conservative"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$abortion_LC))
our.props3$statistic.name = rep("2. Proportion taking party's position on both \nabortion and government aid to Blacks \n(non-Hispanic white respondents only)", length(our.props3$year))
our.props3
new.data5 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT JOBS, AID TO BLACKS: REPUBLICANS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

#######MATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==1 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props1 = prop.table(our.props, 3)
our.props1
our.props1 = as.data.frame(our.props1)
our.props1
our.props1 = subset(our.props1, (aidblacks_LC=="Conservative" & govjobs_LC=="Conservative"))
our.props1
our.props1$our.sample = rep("Matched", length(our.props1$govjobs_LC))
our.props1$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props1$year))
our.props1
#######PARTIALLY MATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0.5 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props2 = prop.table(our.props, 3)
our.props2
our.props2 = as.data.frame(our.props2)
our.props2
our.props2 = subset(our.props2, (aidblacks_LC=="Conservative" & govjobs_LC=="Conservative"))
our.props2
our.props2$our.sample = rep("Partially matched", length(our.props2$govjobs_LC))
our.props2$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props2$year))
our.props2
#######UNMATCHED PARTISANS
our.props = svytable(~ govjobs_LC + aidblacks_LC + year, 
                     design = subset(our.design, programmatic==0 & republican==1 & whiteNH==1))
our.props
# PROPORTION WITH CONSERVATIVE ISSUE POSITIONS ON BOTH
our.props3 = prop.table(our.props, 3)
our.props3
our.props3 = as.data.frame(our.props3)
our.props3
our.props3 = subset(our.props3, (aidblacks_LC=="Conservative" & govjobs_LC=="Conservative"))
our.props3
our.props3$our.sample = rep("Unmatched", length(our.props3$govjobs_LC))
our.props3$statistic.name = rep("1. Proportion taking party's position on both \ngovernment jobs and aid to Blacks \n(non-Hispanic white respondents only)", length(our.props3$year))
our.props3
new.data6 = rbind(our.props1, our.props2, our.props3)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# RENAME 
names(new.data1)[1:2] = c("issue2", "issue1")
names(new.data2)[1:2] = c("issue2", "issue1")
names(new.data3)[1:2] = c("issue2", "issue1")
names(new.data4)[1:2] = c("issue2", "issue1")
names(new.data5)[1:2] = c("issue2", "issue1")
names(new.data6)[1:2] = c("issue2", "issue1")
# MERGE
newer.data = rbind(new.data1, new.data2, new.data3, new.data4, new.data5, new.data6)
newer.data$party = c(rep("2. Democrats", length(newer.data$year)*0.5), 
                     rep("1. Republicans", length(newer.data$year)*0.5))
newer.data$year = as.numeric(as.character(newer.data$year))
newer.data
newer.data$statistic.name = as.factor(newer.data$statistic.name)
levels(newer.data$statistic.name)
levels(newer.data$statistic.name) = c("Proportion taking party's position on both government jobs \nand government aid to Blacks (non-Hispanic white respondents only)",
                                      "Proportion taking party's position on both abortion and \ngovernment aid to Blacks (non-Hispanic white respondents only)",
                                      "Proportion taking party's position on both abortion and government jobs")
newer.data$party = as.factor(newer.data$party)
levels(newer.data$party)
levels(newer.data$party) = c("Republicans", "Democrats")
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# FIGURE 15: PARTISAN IDENTIFIERS' ISSUE CONSTRAINT, 1972-2020, 
# BY MATCHING STATUS
library(tidyverse)
library(ggh4x)
library(lemon)
our.plot = ggplot(newer.data,
                  aes(year, Freq, group = our.sample, 
                      linetype =  our.sample)) +
  geom_line(aes(linetype = our.sample), linewidth = 1) +
  labs(linetype = "Subsample") + 
  xlab("Year") +
  ylab("Proportion \n") +
  theme_minimal() +  
  # scale_color_grey() +
  facet_nested_wrap(~statistic.name + party, nrow = 3, ncol = 2) +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + 
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure15.pdf", width = 6.6, height = 9)
